package me.obstsalatt.guildera2.container;

import me.obstsalatt.guildera2.core.Guild;
import me.obstsalatt.guildera2.core.Owner;
import me.obstsalatt.guildera2.global.Main;
import me.obstsalatt.guildera2.utility.Util;
import org.bukkit.World;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Robin
 * Date: 13.02.14, 23:39
 * Project: GuildEra2
 * Package: me.obstsalatt.guildera2
 * Descriprion:
 * A container storing informations about a world
 */
public class GWorld {
    private World world;
    private Main main;
    private ArrayList<Guild> guilds;
    private ArrayList<GChunk> chunk_assignment;

    public GWorld() {
        this.guilds = new ArrayList<Guild>();
        this.chunk_assignment = new ArrayList<GChunk>();
    }

    public GWorld(Main main, World world) {
        this();
        this.world = world;
        this.main = main;

        try {
            loadChunkAssignments();
            loadGuildAssigns();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void saveWorld() {
        saveChunkAssignments();
        saveGuildAssigns();
    }

    private void loadChunkAssignments() throws IOException {
        String str = Util.readFile(getWorldFilePath() + "chunk_assignments.dat");

        String[] segments = str.split("&");
        String[] subSegments;

        for(String segment : segments) {
            subSegments = segment.split("#");
            if(subSegments.length == 4) {
                chunk_assignment.add(new GChunk(
                        world.getChunkAt(Integer.parseInt(subSegments[0]),  //chunk.x
                                         Integer.parseInt(subSegments[1])), //chunk.z
                        new Owner(subSegments[3],                           //owner.name
                                  Integer.parseInt(subSegments[2]))));      //owner.type
            } else {
                //@TODO >> add debug message
            }
        }
    }
    private void saveChunkAssignments() {
        boolean first = true;
        String segment;
        String base = "";

        for(GChunk c : chunk_assignment) {

            segment = c.getChunk().getX() + "#" + c.getChunk().getZ() + "#" + c.getOwner().getType() + "#" + c.getOwner().getName();

            if(first) {
                first = false;
                base = segment;
            } else {
                base = base + "$" + segment;
            }
        }

        Util.writeFile(getWorldFilePath() + "chunk_assignments.dat", base);
    }
    private void loadGuildAssigns() throws IOException {
        String str = Util.readFile(getWorldFilePath() + "guild_assigns.dat");
        String[] segments = str.split("#");
        Guild g;

        for(String segment : segments) {
            g = main.findGuildByName(segment);
            if(g != null) {
                guilds.add(g);
            }
        }
    }
    private void saveGuildAssigns() {
        String base = "";
        boolean first = true;

        for(Guild g : guilds) {
            if(first) {
                first = false;
                base = g.getName();
            } else {
                base = base + "#" + g.getName();
            }
        }

        Util.writeFile(getWorldFilePath() + "guild_assigns.dat", base);
    }

    public World getWorld() {
        return world;
    }
    public String getWorldFilePath() {
        return  (main.getDataFolder().getAbsolutePath() + File.pathSeparator + "guildera2"
                                                        + File.pathSeparator + "worlds"
                                                        + File.pathSeparator + world.getName()
                                                        + File.pathSeparator);
    }


}
